package com.suneast.suneaststore.modules.personal.wallet;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.suneast.suneaststore.R;
import com.suneast.suneaststore.modules.common.Flag;
import com.suneast.suneaststore.ui.fragments.BaseFragment;

import butterknife.BindView;
import butterknife.OnClick;

public class CoinTopUpFragment extends BaseFragment {
    @Override
    public int setLayout() {
        return R.layout.fragment_coin_top_up;
    }

    private String coinValue = "0.00";

    public CoinTopUpFragment withCoinValue(String value) {
        coinValue = value;
        return this;
    }

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_coin_value)
    TextView coinValueTv;
    @BindView(R.id.cb_wechat_pay)
    CheckBox wechatPayCb;
    @BindView(R.id.cb_ali_pay)
    CheckBox aliPayCb;
    @BindView(R.id.et_top_up_value)
    AppCompatEditText topUpValueEt;
    @BindView(R.id.rv_top_up_trace)
    RecyclerView topUpTraceRv;

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        initView();
    }

    private @Flag.PayType
    int payType;

    private int topUpValue;

    private void initView() {
        toolbar.setNavigationOnClickListener(v -> pop());
        coinValueTv.setText(coinValue);
        wechatPayCb.setOnCheckedChangeListener((CompoundButton buttonView, boolean isChecked) -> {
            if (isChecked) {
                payType = Flag.PayType.WECHAT_PAY;
                aliPayCb.setChecked(false);
            }
        });
        aliPayCb.setOnCheckedChangeListener((CompoundButton buttonView, boolean isChecked) -> {
            if (isChecked) {
                payType = Flag.PayType.ALI_PAY;
                wechatPayCb.setChecked(false);
            }
        });
        topUpValueEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                topUpValue = s.length() > 0 ? Integer.valueOf(s.toString()) : 0;
            }
        });
    }

    @OnClick({R.id.btn_go_to_pay})
    public void onViewClick(View view) {
        if (view.getId() == R.id.btn_go_to_pay) {

            //todo 根据支付方式 将金额作为参数,传给后台
        }
    }
}
